package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysTransfercourseRemoteService;
import com.flyme.module.base.client.entity.SysCertificate;
import com.flyme.module.base.client.entity.SysExamItem;
import com.flyme.module.base.client.entity.SysTransfercourse;
import com.flyme.module.base.provider.service.SysCertificateService;
import com.flyme.module.base.provider.service.SysTransfercourseService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 学分转换课程中间管理
 * @author zyh
 * @date 2020-9-7
 */
@Api(tags = "学分转换课程中间")
@RestController
public class SysTransfercourseController extends BaseController<SysTransfercourseService, SysTransfercourse> implements SysTransfercourseRemoteService {

    @Autowired
    private SysCertificateService certificateService;

    /**
     * 根据证书id重置证书可兑换课程及学分
     */
    @Override
    public ResultBody list(PagerInfo pagerInfo, SysTransfercourse transfercourse) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*");
        cq.eq("certificateId", transfercourse.getCertificateId());
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    /**
     * 证书可兑换课程列表
     */
    @Override
    public ResultBody delById(@PathVariable Long id) {
        Boolean tag = bizService.removeById(id);
        if(tag){
            return ResultBody.success("操作成功");
        }else{
            return ResultBody.failed("操作失败");
        }
    }

    /**
     * 添加/更新证书国开学分课程
     */
    @Override
    public ResultBody addTransfercourse(SysTransfercourse transfercourse) {
        Boolean tag = bizService.saveOrUpdate(transfercourse);
        return ResultBody.success("添加成功").setData(tag);
    }

    /**
     * 重置国开学分课程
     */
    @Override
    public ResultBody resetTransfercourse(@PathVariable Long certificateId) {
        List<SysTransfercourse> list = bizService.list(q().eq("certificateId", certificateId));
        String courseString = "";
        String scoreString = "";
        if(list.size() > 0){
            for(SysTransfercourse item : list){
                courseString =  item.getCourseName() + "," + courseString;
                scoreString =  item.getTransferCredit() + "," + scoreString;
            }
            courseString = courseString.substring(0, courseString.length() - 1);
            scoreString = scoreString.substring(0, scoreString.length() - 1);
        }
        certificateService.update(new UpdateWrapper<SysCertificate>().set("discipline",courseString).
                set("disciplineScore",scoreString).eq("id",certificateId));
        return ResultBody.success("操作成功");
    }
}
